package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.DishFlavor;
import com.sky.entity.Setmeal;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.DishMapper;
import com.sky.mapper.FlavorMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

@Service
public class DishServiceImpl implements DishService {

    @Autowired
    DishMapper dishMapper;

    @Autowired
    FlavorMapper flavorMapper;

    @Autowired
    SetmealDishMapper setmealDishMapper;

    @Autowired
    SetmealMapper setmealMapperl;


    @Transactional
    public void saveWithFlavors(DishDTO dishDTO) {
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO,dish);
        dishMapper.save(dish);

        Long dishId = dish.getId();
        List<DishFlavor> flavors = dishDTO.getFlavors();
        if (flavors==null|| flavors.isEmpty())return;
        for (DishFlavor flavor : flavors) {
            flavor.setDishId(dishId);
            flavorMapper.save(flavor);
        }
    }


    public PageResult pageDish(DishPageQueryDTO dishPageQueryDTO) {
        PageHelper.startPage(dishPageQueryDTO.getPage(),dishPageQueryDTO.getPageSize());
        Page<DishVO> page=dishMapper.pageDish(dishPageQueryDTO);
        return new PageResult(page.getTotal(),page.getResult());
    }


    public void deleteDish(String[] split) {
        for (String s : split) {
            long id = Long.parseLong(s);
            Dish dish=dishMapper.getDishById(id);

            //是否启用
            if (Objects.equals(dish.getStatus(), StatusConstant.ENABLE)){
                throw new DeletionNotAllowedException(MessageConstant.DISH_ON_SALE);
            }

            //是否在套餐中
            List<Long> setmealIdByDishId = setmealDishMapper.getSetmealIdByDishId(id);
            if (setmealIdByDishId!=null&& !setmealIdByDishId.isEmpty()){
                throw new DeletionNotAllowedException(MessageConstant.DISH_BE_RELATED_BY_SETMEAL);
            }

            //删除要将对应口味删除
//            dishMapper.deleteDish(id);
//            flavorMapper.deleteFlavorByDish(id);
        }
        //批量删除要将对应口味删除
        dishMapper.deleteDishs(split);
        flavorMapper.deleteFlavorByDishs(split);
    }


    public DishVO getDishById(Long id) {
        Dish dish=dishMapper.getDishById(id);
        List<DishFlavor> flavors=flavorMapper.getFlavor(id);

        DishVO dishVO=new DishVO();
        BeanUtils.copyProperties(dish,dishVO);
        dishVO.setFlavors(flavors);
        return dishVO;
    }


    public void updateDish(DishDTO dishDTO) {
        //先删除原先关联的口味
        Long dishId=dishDTO.getId();
        flavorMapper.deleteFlavorByDish(dishId);

        Dish dish=new Dish();
        BeanUtils.copyProperties(dishDTO,dish);
        dishMapper.update(dish);

//        Long dishId = dish.getId();
        List<DishFlavor> flavors = dishDTO.getFlavors();
        if (flavors==null|| flavors.isEmpty())return;
        for (DishFlavor flavor : flavors) {
            flavor.setDishId(dishId);
            flavorMapper.save(flavor);
        }
    }


    public void startOrStop(Integer status, Long id) {
        Dish dish = Dish.builder()
                .status(status)
                .id(id)
                .build();
        dishMapper.update(dish);

        if(status==StatusConstant.DISABLE){
            List<Long> setmealIds=setmealDishMapper.getSetmealIdByDishId(id);
            if(setmealIds==null||setmealIds.isEmpty())return;
            for (Long setmealId : setmealIds) {
                Setmeal setmeal = Setmeal.builder()
                        .id(setmealId)
                        .status(status)
                        .build();
                setmealMapperl.update(setmeal);
            }
        }
    }


    public List<Dish> getDishsByCategoryId(Long categoryId) {
        return dishMapper.getDishsByCategoryId(categoryId);
    }


    public List<DishVO> listWithFlavor(Dish dish) {
        List<Dish> dishList = dishMapper.list(dish);

        List<DishVO> dishVOList = new ArrayList<>();

        for (Dish d : dishList) {
            DishVO dishVO = new DishVO();
            BeanUtils.copyProperties(d,dishVO);

            //根据菜品id查询对应的口味
            List<DishFlavor> flavors = flavorMapper.getFlavor(d.getId());

            dishVO.setFlavors(flavors);
            dishVOList.add(dishVO);
        }

        return dishVOList;
    }
}
